#include<stdio.h>
#include <iostream>
#include <thread>
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <cstdio>

using namespace std;
// 1线程创建
   void* threadroutine(void* args)//创建线程后执行的函数
{
    while(1)
    {
        cout<<"hello new thread : "<<(char*)args<<",PID:"<<getpid()<<endl;
        sleep(1);
    }
    
}
int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,threadroutine,(void*)"thread1");
    while(true)
    {
        cout<<" I am main thread,PID:"<<getpid()<<endl;
        sleep(1);
    }
    return 0;
}   
//2 获得tid
 /* void* threadroutine(void* args)
{
    while(1)
    {
       cout<<"new thread running,tid:"<<pthread_self()<<endl;
        sleep(1);
    }
    
}
int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,threadroutine,(void*)"thread1");
   
    while(true)
    {
        cout<<"main thread running,tid:"<<pthread_self()<<endl;

        sleep(1);
    }
    return 0;
} 
  */
// 线程等待
/* void* routine(void* args)
{
    int num=0;
    while(1)
    {
       cout<<"new thread running,tid:"<<pthread_self()<<endl;
       if(num++==10)
       {
               break;
       }
        sleep(1);
    }
    cout<< "new thread  quit"<<endl;
    return (void*)10;
}
int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,routine,(void*)"thread1");
    //pthread_join(tid,nullptr);
      void* ret;
      pthread_join(tid,&ret);

    cout<<"wait new thread success,"<<"return code:"<<(long long)ret<<endl;

   
    /* while(true)
    {
        cout<<"main thread running,tid:"<<pthread_self()<<endl;

        sleep(1);
    } */
  /*   return 0;
} 
 */ 
 
// 线程退出
//return语句
//2 exit()是整个进程退出，线程退出使用pthread_exit()

/* void* routine(void* args)
{
    int num=0;
    while(1)
    {
       cout<<"new thread running,tid:"<<pthread_self()<<endl;
       if(num++==5)
       {
               break;
       }
        sleep(1);
    }
    cout<< "new thread  quit"<<endl;
    pthread_exit((void*)13);
}
int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,routine,(void*)"thread1");
    //pthread_join(tid,nullptr);
      void* ret;
      pthread_join(tid,&ret);

    cout<<"wait new thread success,"<<"exit code:"<<(long long)ret<<endl;
    return 0;
}
 */
//3 pthread_cancel()
//线程被取消，join的时候值为-1
/* void* routine(void* args)
{
   
    while(1)
    {  cout<<"new thread running,tid:"<<pthread_self()<<endl;
       s
       cleep(1);
    } */
  /* 
}
int main()
{
    int count=0;
    pthread_t tid;
    pthread_create(&tid,nullptr,routine,(void*)"thread1");
    while(1)
    {
        count++;
        sleep(1);
        if(count==5)
        break;
    }
    pthread_cancel(tid);
    cout<<"new thread cancel\n";
    
      void* ret;
      pthread_join(tid,&ret);

    cout<<"wait new thread success,"<<"exit code:"<<(long long)ret<<endl;
    return 0;
} */





// _ _thread修饰全局变量，让每一个线程各自拥有一个全局变量，线程的局部存储
//int g_val=0;
/* __thread int g_val=0;
void* routine(void* args)
{
   
    while(1)
    {
        g_val++;
       cout<<"new thread running,tid:"<<pthread_self()<<",g_val:"<<g_val<<",&g_val:"<<&g_val<<endl;
       sleep(1);
    }
  
}
int main()
{
    int count=0;
    pthread_t tid;
    pthread_create(&tid,nullptr,routine,(void*)"thread1");
    while(1)
    {
         cout<<"main thread running,tid:"<<pthread_self()<<",g_val:"<<g_val<<",&g_val:"<<&g_val<<endl;
        count++;
        sleep(1);
        if(count==5)
        break;
    }
    pthread_cancel(tid);
    cout<<"new thread cancel\n";
    
      void* ret;
      pthread_join(tid,&ret);

    cout<<"wait new thread success,"<<"exit code:"<<(long long)ret<<endl;
    return 0;
} */

//线程分离
//线程分离后不能再等待
/* int g_val=0;
void* routine(void* args)
{
   pthread_detach(pthread_self());
    while(1)
    {
        g_val++;
       cout<<"new thread running"<<",g_val:"<<g_val<<endl;
       sleep(1);
       if(g_val==5)
       break;
    }
    pthread_exit((void*)22);
  
}
int main()
{
  
    pthread_t tid;
    pthread_create(&tid,nullptr,routine,(void*)"thread1");
    while(1)
    {
         cout<<"main thread running"<<",g_val:"<<g_val<<endl;
       
        sleep(1);
       
        break;
    }
 
      int n=pthread_join(tid,nullptr);
      cout<<"n:"<<n<<"strerror:"<<strerror(n)<<endl;

   
    return 0;
}
 */

